// index.js
// 获取应用实例
import { getLifeRecords, deleteLifeRecord } from '../../api/timeline'
import { getRecentPictures } from '../../api/album'
const app = getApp()

Page({
  data: {
    iconList: [
      {
        icon: 'tupian',
        color: 'cyan',
        badge: 0,
        name: '相册',
        nav: '/pages/album/album'
      }, {
        icon: 'qianbao',
        color: 'yellow',
        // badge: 22, 
        name: '记账',
        nav: '/pages/expense/expense'
      },
      {
        icon: 'rili',
        color: 'blue',
        badge: 0,
        name: '倒数纪念日',
        nav: '/pages/anniversary/anniversary'
      },
      {
        icon: 'aixinshoucang',
        color: 'pink',
        badge: 0,
        name: '生理期',
        nav: '/pages/Physiological_period/Physiological_period'
      },
    ],
    recentPictures: [],
    result: {},
    liferecords: [],
    query: {
      page: 1
    },
  },
  // 事件处理函数
  onLoad(options) {
    wx.showLoading({ title: '加载中', mask: true })
    this._fetchData(this.data.query).then(res => {
    }).finally(() => wx.hideLoading())
    getRecentPictures().then(res => {
      this.setData({
        recentPictures: res.data
      })
    })
  },

  _fetchData: function (query) {
    return getLifeRecords(query).then(res => {
      this.setData({
        result: res.data,  // 记录分页等查询信息
        liferecords: this.data.liferecords.concat(res.data.results),
      })
      return res.data.results
    }).catch(() => {
      return []
    })
  },
  /**
 * 页面上拉触底事件的处理函数
 */
  onReachBottom: function () {
    let { query, result } = this.data
    if (result.has_next) {
      query['page'] = result.next
      this.setData({ loadMoreStatus: 'loading' })
      this._fetchData(query).then(res => {
        this.setData({ loadMoreStatus: 'nomore' })
      }).catch(() => this.setData({ loadMoreStatus: 'hidding' }))
    }
  },
  // 删除
  deleteLifeRecordTap(res) {
    console.log(res)
    deleteLifeRecord(res.currentTarget.dataset.id).then(res => {
      this.setData({
        result: {},
        liferecords: []
      })
      this._fetchData({
        page: 1
      }).then(res => {
      }).finally(() => wx.hideLoading())
    })
  }

})
